package cn.cque.controller;

import cn.cque.dao.OrderItemDao;
import cn.cque.entity.*;
import cn.cque.service.CategoryService;
import cn.cque.service.OrderService;
import cn.cque.service.ProductService;
import cn.cque.service.UserService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: XWC
 * @ClassName: AdminController
 * @Date: 2020/10/5
 * @Time: 15:55
 * @Description:
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    CategoryService categoryService;

    @Autowired
    ProductService productService;

    @Autowired
    OrderService orderService;

    @Autowired
    UserService userService;

    @Autowired
    OrderItemDao orderItemDao;

    Gson gson=new Gson();

    //主页跳转
    @RequestMapping("/index")
    public String index() {
        return "/admin/index";
    }

    //商品分类管理界面
    @RequestMapping("/category/list")
    public ModelAndView categoryList() {
        ModelAndView modelAndView = new ModelAndView();
        //查询所有商品分类
        List<Category> categoryList = categoryService.findAll();
        //将数据封装到前端页面
        modelAndView.addObject("categoryList", categoryList);
        modelAndView.setViewName("/admin/category-list");

        return modelAndView;
    }

    //添加商品分类
    @RequestMapping("/category/add")
    @ResponseBody
    public String addCategory(Category category) {
        //添加操作
        Integer i = categoryService.addCategory(category);
        if (i > 0) {
            List<Category> categoryList = categoryService.findAll();
            String json = gson.toJson(categoryList);
            return json;
        }
        return "添加失败";
    }

    //删除商品分类
    @RequestMapping("/category/delete")
    @ResponseBody
    public String deleteCategory(String cid) {
        //删除操作
        Integer i = categoryService.deleteCategory(cid);
        if (i > 0) {
            List<Category> categoryList = categoryService.findAll();
            String json = gson.toJson(categoryList);
            return json;
        }
        return "删除失败";
    }

    //修改时查询修改的分类
    @RequestMapping("/category/findOne")
    @ResponseBody
    public String findCategoryById(String cid) {
        Category category = categoryService.findById(cid);
        //将数据封装为json数据返回前端
        String json = gson.toJson(category);
        return json;
    }

    //修改操作
    @RequestMapping("/category/update")
    @ResponseBody
    public String updateCategory(Category category) {
        Integer i = categoryService.updateCategory(category);
        if (i > 0) {
            List<Category> categoryList = categoryService.findAll();
            String json = gson.toJson(categoryList);
            return json;
        }
        return "修改失败";
    }

    //商品管理界面
    @RequestMapping("/product/list")
    public ModelAndView productList() {
        ModelAndView modelAndView = new ModelAndView();
        //查询所有商品
        List<Product> productList = productService.findAll();

        List<Category> categoryList = categoryService.findAll();
        //将数据封装到前端页面
        modelAndView.addObject("productList", productList);
        modelAndView.addObject("categoryList", categoryList);
        modelAndView.setViewName("/admin/product-list");
        return modelAndView;
    }

    //增加商品
    @RequestMapping("/product/add")
    @ResponseBody
    public String addProduct(Product product) {
        System.out.println(product);
        Integer i = productService.addProduct(product);
        if (i > 0) {
            List<Product> productList = productService.findAll();
            List<Category> categoryList = categoryService.findAll();
            Map<String, List> map=new HashMap<>();
            map.put("productList",productList);
            map.put("categoryList",categoryList);
            String json = gson.toJson(map);
            return json;
        }
        return "添加失败";
    }

    //删除商品
    @RequestMapping("/product/delete")
    @ResponseBody
    public String deleteProduct(String id) {
        System.out.println(id);
        Integer i = productService.deleteProduct(id);
        if (i > 0) {
            List<Product> productList = productService.findAll();
            List<Category> categoryList = categoryService.findAll();
            Map<String, List> map=new HashMap<>();
            map.put("productList",productList);
            map.put("categoryList",categoryList);
            String json = gson.toJson(map);
            return json;
        }
        return "删除失败";
    }

    //修改商品信息的查询
    @RequestMapping("/product/findOne")
    @ResponseBody
    public String findProductById(String id) {
        System.out.println(id);
        Product product = productService.findById(id);
        String json = gson.toJson(product);
        return json;
    }

    //修改查询到的商品信息
    @RequestMapping("/product/update")
    @ResponseBody
    public String updateProduct(Product product) {
        Integer i = productService.updateProduct(product);
        if (i > 0) {
            List<Product> productList = productService.findAll();
            List<Category> categoryList = categoryService.findAll();
            Map<String, List> map=new HashMap<>();
            map.put("productList",productList);
            map.put("categoryList",categoryList);
            String json = gson.toJson(map);
            return json;
        }
        return "修改失败";
    }

    //订单列表界面
    @RequestMapping("/order/list")
    public ModelAndView orderList() {
        ModelAndView mv = new ModelAndView();
        List<Order> orderList = orderService.findAll();
        List<User> userList = userService.findAll();
        mv.addObject("orderList", orderList);
        mv.addObject("userList", userList);
        mv.setViewName("/admin/order-list");
        return mv;
    }

    //修改订单时的查询
    @RequestMapping("/order/findOne")
    @ResponseBody
    public String findOrderById(String id) {
        Order order = orderService.findById(id);
        String json = gson.toJson(order);
        return json;
    }

    //修改订单
    @RequestMapping("/order/update")
    @ResponseBody
    public String updateOrder(Order order) {
        //修改订单
        Integer i = orderService.updateOrder(order);
        if (i > 0) {
            List<Order> orderList = orderService.findAll();
            List<User> userList = userService.findAll();
            Map<String,List> map=new HashMap<>();
            map.put("orderList",orderList);
            map.put("userList",userList);
            String json = gson.toJson(map);
            return json;
        }
        return "修改失败";
    }

    //查询一个订单下的所有订单项
    @RequestMapping("/order/findOrderList")
    @ResponseBody
    public String findOrderList(String id) {
        List<OrderItem> orderItemList = orderItemDao.findListByOid(id);
        //封装为json数据返回前端
        String json = gson.toJson(orderItemList);
        return json;
    }

    //删除订单项
    @RequestMapping("/orderItem/delete")
    @ResponseBody
    public String deleteOrderItem(String oid, String id) {
        //删除订单项
        Integer i = orderItemDao.deleteItemById(id);
        if (i > 0) {
            //重新查询所有订单项，将数据返回前端
            List<OrderItem> orderItemList = orderItemDao.findListByOid(oid);
            String json = gson.toJson(orderItemList);
            return json;
        }
        return "删除失败";
    }

    //删除订单
    @RequestMapping("/order/delete")
    @ResponseBody
    public String deleteOrder(String id) {
        //删除订单
        Integer i = orderService.deleteOrder(id);
        if (i > 0) {
            List<Order> orderList = orderService.findAll();
            List<User> userList = userService.findAll();
            Map<String,List> map=new HashMap<>();
            map.put("orderList",orderList);
            map.put("userList",userList);
            String json = gson.toJson(map);
            return json;
        }
        return "删除失败";
    }
}